using System;
using System.Collections.Generic;
using System.Text;
using AF_ORM.CORE;
using System.Data;
using Bussiness.Config;
using System.Data.SqlClient;

namespace Bussiness.Manager
{


    public class CmsProductsManager : Bussiness.ManagerBase.CmsProductsManagerBase
    {
        public static CmsProductsManager CreateInstant()
        {
            return new CmsProductsManager();
        }

        public DataTable Search(string productName, Guid cat,string language)
        {
            
            string sql = "select * from viewProduct where 1=1 ";
            if (!string.IsNullOrEmpty(productName))
            {
                if (language == "vi-VN")
                    sql += " and Title_vn like N'%" + productName + "%'";
                else
                    sql += " and Title_en like N'%" + productName + "%'";
            }
            if (cat != Guid.Empty)
            {                
               sql += " and CategoryId ='" + cat + "'";
            }
            return SqlHelper.ExecuteDataTable(Config.ConnectionConfig.ConnectionString, CommandType.Text, sql);
        }

//        public DataTable GetProductByCategory(Guid CategoryID)
//        {
//            string strSQL = @"Select pro.*,pros.Photo,pros.Price From cmsProductInfos pro
//                                  INNER JOIN cmsProducts pros ON pro.ProductId = pros.Id
//                                   Where pros.CategoryId=@CategoryID";
//            SqlParameter[] param = new SqlParameter[1];
//            param[0] = new SqlParameter("CategoryID", CategoryID);
//            return WB.Library.SqlHelper.ExecuteDataTable(ConnectionConfig.ConnectionString, CommandType.Text, strSQL, param);
//        }
        /// <summary>
        /// Lay danh sach san pham theo IntId cua category
        /// </summary>
        /// <param name="intID"></param>
        /// <param name="lang"></param>
        /// <returns></returns>
        public DataTable GetProductByCategory(int intID,string lang)
        {
            string strSQL = @"select *
                            from viewProduct P 
								INNER JOIN viewProductCategory C on P.CategoryId = C.Id_VN or P.CategoryId = C.Id_En							
                            where C.IntId = @intID

                        ";
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@intId", intID);
            return WB.Library.SqlHelper.ExecuteDataTable(ConnectionConfig.ConnectionString, CommandType.Text, strSQL, param);
        }

        public DataTable GetProductByCategoryTOP(int intID, string lang, string top,int Product)
        {
            string u = string.Empty;
            if (!string.IsNullOrEmpty(top))
            u = " top " + top + " ";
            string strSQL = @"select " + u + @" P.*,C.IntId as ProductID
                            from viewProduct P 
								INNER JOIN viewProductCategory C on P.CategoryId = C.Id_VN or P.CategoryId = C.Id_En							
                            where C.IntId = @intID 
                        ";
            if (Product != -1)
                strSQL += " and P.IntId NOT IN("+Product+")";
            
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@intId", intID);
            return WB.Library.SqlHelper.ExecuteDataTable(ConnectionConfig.ConnectionString, CommandType.Text, strSQL, param);
        }
        /// <summary>
        /// Lay danh sach category san pham
        /// </summary>
        /// <param name="lang"></param>
        /// <returns></returns>
        public DataTable GetProductCategory(string lang)
        {
            string sql = "select * from viewProductCategory ";
            return WB.Library.SqlHelper.ExecuteDataTable(ConnectionConfig.ConnectionString, CommandType.Text, sql);
        }

        public DataTable GetProductInfoID(int ProductID) {
            
            string strSQL = "select * from viewProduct ";
            if(ProductID != -1)
                strSQL +=" where Intid=@ProductID";
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("ProductID",ProductID);
            return WB.Library.SqlHelper.ExecuteDataTable(ConnectionConfig.ConnectionString,CommandType.Text,strSQL,param);
        }

        
    }
}
                    
